﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;


namespace Trade.DBUtility
{
    public  class UserPowerCondition
    {
        public string GetUserPowerCondition(string strUserCode)
        {
            string strUserPowerString = string.Empty;
            SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);

            try
            {
                //打开数据库连接
                sqlConn.Open();


                //获取用户信息
                string strSqlUser = "select * from T_User where UserCode='" + strUserCode + "'";
                SqlCommand sqlComUser;
                SqlDataAdapter sqlAdaUser;
                DataSet dsUser = new DataSet();
                sqlComUser = new SqlCommand(strSqlUser, sqlConn);
                sqlAdaUser = new SqlDataAdapter(sqlComUser);
                sqlAdaUser.Fill(dsUser, "T_User");

                #region 获取权限组中下辖用户的列表（超级管理员，最高权限）

                //权限－下辖成员权限编号
                List<string> lstManagerUser = new List<string>();
                //权限组、下辖成员子集、单个下辖成员
                string strUserPowerGroup = string.Empty;
                string strUserCodeList = string.Empty;
                string strManagerUserS = string.Empty;

                string isHighPower = dsUser.Tables[0].DefaultView[0]["ISHighPower"].ToString();
                //如果是超级管理员：最高权限；
                if (isHighPower == "是")
                {
                    string strSqlAll = "select * from T_User";
                    SqlCommand sqlComAll;
                    SqlDataAdapter sqlAdaAll;
                    DataSet dsAll = new DataSet();
                    sqlComAll = new SqlCommand(strSqlAll, sqlConn);
                    sqlAdaAll = new SqlDataAdapter(sqlComAll);
                    sqlAdaAll.Fill(dsAll, "T_All");
                    foreach (DataRowView drv in dsAll.Tables[0].DefaultView)
                    {
                        lstManagerUser.Add(drv["UserCode"].ToString().ToLower());
                    }
                }
                else
                {
                    if (dsUser.Tables.Count > 0 && dsUser.Tables[0].Rows.Count > 0)
                    {
                        strUserPowerGroup = dsUser.Tables[0].DefaultView[0]["UserPowerGroup"].ToString();
                        strUserCodeList = dsUser.Tables[0].DefaultView[0]["UserCodeList"].ToString();
                    }
                    if (string.IsNullOrEmpty(strUserPowerGroup.Trim()))
                    {
                        //没有从组继承权限
                        strManagerUserS = strUserCodeList;
                    }
                    else
                    {
                        //如果继承了组权限
                        string strSQLGroup = "SELECT * FROM T_UserPowerGroup WHERE UserPowerGroup='" + strUserPowerGroup + "'";
                        SqlCommand sqlComGroup;
                        SqlDataAdapter sqlAdaGroup;
                        DataSet dsGroup = new DataSet();
                        sqlComGroup = new SqlCommand(strSQLGroup, sqlConn);
                        sqlAdaGroup = new SqlDataAdapter(sqlComGroup);
                        sqlAdaGroup.Fill(dsGroup, "T_PowerGroup");
                        strManagerUserS = dsGroup.Tables[0].DefaultView[0]["UserCodeList"].ToString();
                    }
                    string[] strS = strManagerUserS.Split('|');
                    foreach (string str in strS)
                    {
                        if (str.Trim() == string.Empty)
                            continue;
                        lstManagerUser.Add(str);
                    }
                    if (lstManagerUser.IndexOf(strUserCode) < 0)
                        lstManagerUser.Add(strUserCode);
                }
                #endregion

                //分解合成下辖成员集合
                string strUserPower = string.Empty;

                #region 分解合成UserPowerConditon

                string strWhere1 = "";
                string strWhere2 = "";

                //这个地方是有值的"001|002|001|002|001|002|001|002|001|002|001|002";所以重置一下
                strManagerUserS = "";
                for (int i = 0; i < lstManagerUser.Count; i++)
                {
                    if (strManagerUserS == "")
                        strManagerUserS = "'" + lstManagerUser[i] + "'";
                    else
                        strManagerUserS = strManagerUserS + ",'" + lstManagerUser[i] + "'";
                }
                strManagerUserS = "(" + strManagerUserS + ")";
                string strENColumnName = "Creater";
                if (strWhere1 == "")
                    strWhere1 = strENColumnName + "='" + strUserCode + "'";
                else
                    strWhere1 = strWhere1 + " OR " + strENColumnName + "='" + strUserCode + "'";

                if (strWhere2 == "")
                    strWhere2 = strENColumnName + " IN " + strManagerUserS;
                else
                    strWhere2 = strWhere2 + " AND " + strENColumnName + " IN " + strManagerUserS;

                strENColumnName = "UserCode";
                if (strWhere1 == "")
                    strWhere1 = strENColumnName + "='" + strUserCode + "'";
                else
                    strWhere1 = strWhere1 + " OR " + strENColumnName + "='" + strUserCode + "'";
                if (strWhere2 == "")
                    strWhere2 = strENColumnName + " IN " + strManagerUserS;
                else
                    strWhere2 = strWhere2 + " OR " + strENColumnName + " IN " + strManagerUserS;

                if (String.IsNullOrEmpty(strWhere1) == false && String.IsNullOrEmpty(strWhere1) == false)
                    strUserPower = " ((" + strWhere1 + ") OR (" + strWhere2 + ")) ";

                #endregion

                strUserPowerString = strUserPower;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally 
            {
                if (sqlConn.State == ConnectionState.Open)
                    sqlConn.Close();
            }
            return strUserPowerString;
        }

    }
}
